<?php
namespace app\wx\controller;
use think\Request;
use think\Controller;
use think\Db;


class Index5 extends Controller
{

	/**
 * 接收code 获得openid
 * https://www.hyhthjk.com.cn/note.php/wx/Index5/index4?code=0015PvyP1aA0o21wHvAP1u7AyP15PvyG
 */
public function index4(){
	$code = input('get.code');
	// $this->logger($code);
	$appid = "wx3843c7001aba60e4";   // 对接 念佛群 小程序
	$secret = "8ba50984c338480a59566a6487e583f7";
	$api = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
	$ch = curl_init();                      //向微信服务器请求
	curl_setopt($ch, CURLOPT_URL,$api);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
	$data1 = curl_exec($ch);
	curl_close($ch);
	$data = json_decode($data1,true);  
	// dump($data);  //获得 session_key 和openid
	  
	$openid = $data['openid'];
   // $this->logger($data);
   // 	return json($openid);
	//去kehu表 按openid取出信息，能取出，则去念佛表取出念佛信息getmassage() 取不出来则创建客户 和念佛表 再重复取出的动作getmassage()
	 $arr = $this->getmessage($openid);
     return json($arr);
}


/**
 * https://www.hyhthjk.com.cn/note.php/wx/Index5/getmessage?openid=o55Ia0VM8IJvvw5hJ3fiCnVJuSjo
 */
public function getmessage($openid){
  //去kehu表 按openid取出信息，能取出，则去念佛表取出念佛信息getmassage() 取不出来则创建客户 和念佛表 再重复取出的动作getmassage() 记录report
    $data = $this->select2($openid);
    // dump($data);  //没有就返回 array(0){}
    if(empty($data)) {
     //获得用户详细信息
      return '向腾讯要详细信息';

      // echo'创建客户 和念佛表 ps_jifen 表';
    //  $this->creat1($openid);  // 用初发心小菩萨+id 创建客户表
    //  $this->creat3($openid);    // 创建念佛表
    //  $this->creat5($openid);       // 创建ps_jifen 计分表 （）
    };
    //从念佛表 客户表 取出信息	 记录report
  
     $data = $this->select2($openid);
     $arr['headimgurl'] = $data['0']['headimgurl'];
     $arr['nickname'] = $data['0']['nickname'];
     $arr['openid'] = $data['0']['openid'];
     $data = $this->select3($openid);  //查今天念佛数
     // dump($openid);
     // dump($data);
     $arr['today_nfnub'] = $data['0']['today_nub'];
     $url = "pages/note/index";
     $this->creat2($openid,$url,$arr['nickname']);  //记录登陆
     // dump($arr);
     return $arr;
  
  }


  /**
  *  https://www.hyhthjk.com.cn/note.php/wx/Index5/select2?openid=oUwuW1eOmL_qM2-WZHbSR3T-1HbE
  *
  */
public function select2($openid){
	$where['openid'] = $openid;
	  return Db::table('note_kehu')->where($where)->select();
	}

    }

 